import apiClient from "../apiClient";

import type { UserInfo, UserToken } from "#/entity";

export interface SignInReq {
	username: string;
	password: string;
	remember?: boolean;
}

export interface SignUpReq extends SignInReq {
	email: string;
}
export type SignInRes = UserToken & { user: UserInfo };

export interface UserItem {
	id: number;
	nickname: string;
	mobile: string;
	createtime: number;
	updatetime: number;
	status: number;
	company: string;
	alipay_account: string | null;
	wechat_account: string | null;
	business_license_img: string;
	wechat_qr_code_img: string | null;
	alipay_qr_code_img: string | null;
	remark: string;
	website_logo: string;
}

export interface UserAddParams {
	username: string;
	password: string;
	nickname: string;
	mobile: string;
	status: string;
}

export interface UserEditParams {
	id: number;
	nickname?: string;
	mobile?: string;
	status?: string;
}

export interface UserSearchParams {
	mobile?: string;
	nickname?: string;
	status?: number;
}

export interface UserListResponse {
	total: number;
	items: UserItem[];
}

export interface UserStatusParams {
	id: number;
	status: number;
}

export enum UserApi {
	SignIn = "/index/login",
	SignUp = "/auth/signup",
	Logout = "/auth/logout",
	Refresh = "/auth/refresh",
	User = "/user",
	// 用户管理接口
	UserList = "/user/user/index",
	UserAdd = "/user/user/add",
	UserEdit = "/user/user/edit",
	UserDelete = "/user/user/delete",
	UserDetail = "/user/detail", // 新增用户详情接口
	UserChangeStatus = "/user/user/changeStatus",
}

const signin = (data: SignInReq) =>
	apiClient.post<SignInRes>({ url: UserApi.SignIn, data });
const signup = (data: SignUpReq) =>
	apiClient.post<SignInRes>({ url: UserApi.SignUp, data });
const logout = () => apiClient.get({ url: UserApi.Logout });
const findById = (id: string) =>
	apiClient.get<UserInfo[]>({ url: `${UserApi.User}/${id}` });

// 用户管理接口
const userList = (params?: UserSearchParams) =>
	apiClient.get<UserListResponse>({ url: UserApi.UserList, params });

const userDetail = (id: number) =>
	apiClient.get<UserItem>({ url: `${UserApi.UserDetail}/${id}` });

const userAdd = (data: UserAddParams) =>
	apiClient.post({ url: UserApi.UserAdd, data });

const userEdit = (data: UserEditParams) =>
	apiClient.post({ url: UserApi.UserEdit, data });

const userDelete = (id: number) =>
	apiClient.post({ url: UserApi.UserDelete, data: { id } });

// 修改用户状态
const userChangeStatus = (data: UserStatusParams) =>
	apiClient.post<null>({ url: UserApi.UserChangeStatus, data });

export default {
	signin,
	signup,
	findById,
	logout,
	// 用户管理接口
	userList,
	userAdd,
	userEdit,
	userDelete,
	userDetail,
	userChangeStatus,
};
